package com.example.mapsapp;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.util.Log;

public class PostRequestTask extends AsyncTask <String, String, String> {
   public static String set;
   
   public static final String QUERY_STRING=   "http://softeng.cs.uwosh.edu/students/cs342g2/derek.php?";
	@Override
	protected String doInBackground(String... params) {
		XmlPullParser batman = tryDown(params[0],params[1]);
		 try {
			 Log.i("Request failed5", "these is it  in background");
			set = trygeMessage(batman);
			
		} catch (XmlPullParserException e) {
			// TODO Auto-generated catch block
			 Log.i("Request failed5", "these is it  in background");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			 Log.i("Request failed5", "these is it  in background");
		}
		
		return set;
	}
	private XmlPullParser tryDown(String d,String r)
	   {
		System.out.println("firstparam "+ d+"  Second param "+ r);
		try{
		 StringBuilder b = new StringBuilder(QUERY_STRING);
	       b.append("user=");
	       b.append(d);
              b.append("&password=");
              b.append(r);
	       URI url  = new URI(b.toString());
	       DefaultHttpClient client = new DefaultHttpClient();

	       HttpGet method = new HttpGet(url);

	       HttpResponse res = client.execute(method);

	      
		  XmlPullParser superman = XmlPullParserFactory.newInstance().newPullParser();
		  superman.setInput(new InputStreamReader(res.getEntity().getContent()));
		  Log.i("Request failed3", "ok in first method");
		 

		  return superman;
		}
		catch(XmlPullParserException e)
		{
			 
			Log.i("Request failed1", e.toString());
		}
		catch(IOException e)
		{
			
			Log.i("Request failed2", e.toString());
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			Log.i("Request failed3", e.toString());
			//e.printStackTrace();
			
		}
		
		return null;
	    }

	private String trygeMessage(XmlPullParser batman) throws XmlPullParserException, IOException {
		 String results = " ";
			
	      
		    int eventType;			 
			eventType = batman.getEventType();
			String currentTag = null;
	        String title = null;
	        boolean done = false;
	        String TagTest = null;
	        int i  = 0;
	        while (eventType != XmlPullParser.END_DOCUMENT) {
	        	    
	        	     
	            if(eventType == XmlPullParser.START_DOCUMENT) {
	                 
	                System.out.println("Start document");
	            } else if(eventType == XmlPullParser.END_DOCUMENT) {
	            	
	                System.out.println("End document");
	            } else if(eventType == XmlPullParser.START_TAG) {
	            	TagTest = batman.getName();
	                System.out.println("Start tag "+batman.getName());
	            } else if(eventType == XmlPullParser.END_TAG) {
	            	   TagTest =" ";
	            	   
	            	  }
	               
	             else if(eventType == XmlPullParser.TEXT) {
	            	if (TagTest.equals("User"))
	            	  {
	            		results = batman.getText();
	            		 System.out.println(results);
	            	  }
	            	
	           	  
	                 
	            }
	             i++;
	            eventType = batman.next();
	           }     
	       
	        
	          
	        return results;
	}

	 @Override
		protected void onPostExecute(String result)
		{
		 System.out.println(result);
			
	    
		}  
		
		
	}
